package lxthinkingdatas

import (
	"encoding/json"
	"fmt"
	"gitee.com/lxgow/lxconv"
	"io/ioutil"
	"net/http"
	"strings"
)

// 分群或标签
// 创建时间:2024/1/24 11:26
// 创建人:lixu

const (
	CLUSTER_BY_DYNAMIC_CONDITION = "cluster_by_dynamic_condition" //动态条件更新用户群
	CLUSTER_BY_STATIC_CONDITION  = "cluster_by_static_condition"  //静态条件用户群
	CLUSTER_BY_RESULT            = "cluster_by_result"            //结果分群
	CLUSTER_BY_IMPORT            = "cluster_by_import"            //自定义上传 ID 分群
	CLUSTER_BY_DYNAMIC_SQL       = "cluster_by_dynamic_sql"       //动态SQL分群
	CLUSTER_BY_STATIC_SQL        = "cluster_by_static_sql"        //静态SQL分群
	TAG_BY_DYNAMIC_CONDITION     = "tag_by_dynamic_condition"     //动态条件更新用户标签
	TAG_BY_STATIC_CONDITION      = "tag_by_static_condition"      //静态条件用户标签
	TAG_BY_IMPORT                = "tag_by_import"                //自定义上传 ID 标签
	TAG_BY_DYNAMIC_SQL           = "tag_by_dynamic_sql"           //动态SQL标签
	TAG_BY_STATIC_SQL            = "tag_by_static_sql"            //静态SQL标签
)

type MRequestClusterList struct {
	ClusterCatalog string                     `json:"clusterCatalog"`
	ClusterName    string                     `json:"clusterName"`
	ClusterTypes   []string                   `json:"clusterTypes"`
	PagerHeader    MRequestClusterPagerHeader `json:"pagerHeader"`
	ProjectID      int                        `json:"projectId"`
}

type MRequestClusterPagerHeader struct {
	PageNum  int `json:"pageNum"`
	PageSize int `json:"pageSize"`
}

type MRespClusterListInfo struct {
	Data struct {
		PagerResult struct {
			PageNum  int `json:"pageNum"`
			PageSize int `json:"pageSize"`
			TotalNum int `json:"totalNum"`
		} `json:"pagerResult"`
		UserClusters []MUserClusters `json:"userClusters"`
	} `json:"data"`
	ReturnCode       int    `json:"return_code"`
	ReturnMessage    string `json:"return_message"`
	ShowStackMessage bool   `json:"showStackMessage"`
}

type MUserClusters struct {
	ClusterCatalog           string `json:"clusterCatalog"`
	ClusterName              string `json:"clusterName"`
	ClusterType              string `json:"clusterType"`
	DisplayName              string `json:"displayName"`
	EntityColumnName         string `json:"entityColumnName"`
	EntityID                 int    `json:"entityId"`
	EntityName               string `json:"entityName"`
	HistoryDataStatus        int    `json:"historyDataStatus"`
	ID                       int    `json:"id"`
	MainColumnName           string `json:"mainColumnName"`
	Progress                 int    `json:"progress"`
	ProjectID                int    `json:"projectId"`
	RefreshDelay             int    `json:"refreshDelay"`
	RefreshTime              string `json:"refreshTime"`
	Remarks                  string `json:"remarks"`
	SelectType               string `json:"selectType"`
	Status                   int    `json:"status"`
	SubConditionTabType      string `json:"subConditionTabType,omitempty"`
	UploadFileName           string `json:"uploadFileName"`
	UserID                   int    `json:"userId"`
	UserName                 string `json:"userName"`
	UsersNum                 int    `json:"usersNum"`
	ImportIDClusterAddRspDTO struct {
		ClusterID       int `json:"clusterId"`
		TagValueNullNum int `json:"tagValueNullNum"`
		UnmatchedNum    int `json:"unmatchedNum"`
		UploadNum       int `json:"uploadNum"`
		UserNum         int `json:"userNum"`
	} `json:"importIdClusterAddRspDTO,omitempty"`
	TagStats []struct {
		TagValue string `json:"tagValue"`
		UserNum  int    `json:"userNum"`
	} `json:"tagStats,omitempty"`
}

// UserClusterList 分群和标签列表查询
func UserClusterList(jsonBody MRequestClusterList, config MQueryConfig) (info MRespClusterListInfo, bodys []byte, err error) {

	config.ApiUrl = strings.Replace(config.ApiUrl, "https://", "http://", -1)

	url := config.ApiUrl + "/open/user-cluster-list?token=" + config.Token
	method := "POST"

	payload := strings.NewReader(lxconv.JsonEncode(jsonBody))

	client := &http.Client{}
	req, err := http.NewRequest(method, url, payload)

	if err != nil {
		err = fmt.Errorf("http.NewRequest error::%s", err.Error())
		return
	}

	req.Header.Add("Content-Type", "application/json")

	res, err := client.Do(req)
	if err != nil {
		err = fmt.Errorf("client.Do error::%s", err.Error())
		return
	}

	defer res.Body.Close()

	bodys, err = ioutil.ReadAll(res.Body)
	if err != nil {
		err = fmt.Errorf("ioutil.ReadAll error::%s", err.Error())
		return
	}

	_ = json.Unmarshal(bodys, &info)

	return
}
